package com.lord.wand.common;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 对控制层 抛出的异常 统一处理
 * 
 * @author xuyh
 *
 */
@Component
@Aspect
public class ServiceAspect {

	public static final Logger logger = LoggerFactory.getLogger(ServiceAspect.class);

	@Pointcut("execution(public * com.lord..*.*Controller.*(..))")
	public void tangentPoint() {
	};

	@Around("tangentPoint()")
	public Object handleControllerMethod(ProceedingJoinPoint pjp) {
//		logger.info(pjp.getTarget().getClass().get);
		
		
		StopWatch stopwatch = new StopWatch();
		Object data = null;
		try {
			stopwatch.start();
			data = pjp.proceed();
			stopwatch.stop();
			logger.info("执行方法：{}，耗时：{}ms（毫秒）", pjp.getSignature(),     
					 stopwatch.getTotalTimeMillis());
		} catch (Throwable throwable) {
			handlerException(pjp, throwable);
		}
		return data;
	}

	private void handlerException(ProceedingJoinPoint pjp, Throwable e) {
		if (e instanceof RuntimeException) {
			// logger.error("RuntimeException{方法：" + pjp.getSignature() + "， 参数：" +
			// pjp.getArgs() + ",异常：" + e.getMessage()
			// + "}", e);
		} else {
			// logger.error("异常{方法：" + pjp.getSignature() + "， 参数：" + pjp.getArgs() + ",异常："
			// + e.getMessage() + "}", e);
		}
		e.printStackTrace();
	}

}
